package cn.itsource.org.service.impl;

import cn.itsource.basic.utils.PageList;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    //查询所有
    @Override
    public List<Employee> findAll() {
        return employeeMapper.selectAll();
    }

    //根据id查询一个部门对象
    @Override
    public Employee findOne(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    //查询所有部门经理
    @Override
    public List<Employee> findManagers() {
        return employeeMapper.findManager();
    }


    //新增或修改
    @Override
    public void saveOrUpdate(Employee employee) {
        if(employee.getId()==null){
            employeeMapper.insertSelective(employee);
        }else {
            employeeMapper.updateByPrimaryKeySelective(employee);
        }

    }


    //分页查询
    @Override
    public PageList<Employee> findPage(EmployeeQuery query) {
        //调用分页方法
        PageHelper.startPage(query.getPage(),query.getSize());
        //调用查询方法
        List<Employee> employees = employeeMapper.selectAll();
        //封装pageInfo对象
        PageInfo<Employee> pageInfo = new PageInfo<>(employees);
        //封装自定义对象
        PageList<Employee> pageList = new PageList<>(pageInfo.getTotal(), pageInfo.getList());
        //返回分页数据
        return pageList;
    }

    //删除
    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
    }


}
